import { fileURLToPath, URL } from 'node:url'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig(({ command }) => {
  // 根据命令加载对应的环境变量文件
  const env = loadEnv(command, process.cwd());

  return {
    base: './',
    plugins: [
      vue(),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    server: {
      open: false, //项目启东时是否打开页面
      host: "127.0.0.1",
      port: 3457,
      proxy: {
        "^/api/": {
          target: env.VITE_API_DOMAIN, // 从环境变量中获取
          changeOrigin: true /* 允许跨域 */,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
      },
    },
  };
});
